<template>
    <el-dialog v-model="props.show" title="编辑用户" width="40%">
      <el-form :model="user" label-width="100px" >
        <el-form-item label="手机号">
          <el-input v-model="user.phone" />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="user.name" />
        </el-form-item>
        <el-form-item label="是否是主理人">
          <el-switch v-model="isCompere" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary"  @click="submit">提交</el-button>
          <el-button  @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </template>
  
  <script setup lang="ts">

import { defineProps, defineEmits,computed,getCurrentInstance} from 'vue'
  interface User{
    id?: number
    image: string
    phone: string
    name: string
    compere: number
    createTime?: string
    updateTime?: string
  }

  interface Props {
    show: boolean
    user: User
    type:number
  }
  let isCompere = computed({
  get: () => Boolean(props.user.compere),
  set: (value) => {
    props.user.compere = value ? 1 : 0
  }
})
const { proxy } = getCurrentInstance() as { proxy: any };
  const emit = defineEmits(['refresh', 'close'])
  const props = withDefaults(defineProps<Props>(), {
		show:false,
    user: () => ({
      id: 0,
      image: '',
      phone: '',
      name: '',
      compere: 0,
      createTime: "",
      updateTime: ""
    })
	});

  const close=():void=>{
    emit("close")
  }
  //提交  
  const submit = () => {
    if(props.type===0){
      addUser()
    }else{
      updataUser()
    }
  }

  // 添加会员
  const addUser=():void=>{
    proxy.api.user.add({
      id:props.user.id,
      phone:props.user.phone,
      name:props.user.name,
      compere:isCompere?1:0
    }).then((rsp:any)=>{    
      if(rsp.code==200){
        close()
        emit("refresh")
        
      }
    })
  }

  //修改用户信息
  const updataUser=():void=>{
    proxy.api.user.updata({
      id:props.user.id,
      phone:props.user.phone,
      name:props.user.name,
      compere:isCompere?1:0
    }).then((rsp:any)=>{
      console.log(rsp,"======")
     
      if(rsp.code==200){
        close()
        emit("refresh")
        
      }
    })
  }

  </script>